package lanou.autohome.recommend.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import lanou.autohome.R;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.recommend.beans.GachincoBean;

/**
 * Created by dllo on 16/8/17.
 */
public class GachincoAdapter extends BaseAdapter {

    private int TYPE_THREE_IMAGE = 2;
    private int TYPE_ONE_IMAGE = 1;

    private Context mcontext;
    private GachincoBean gachincoBean;
    private int newType ;

    public GachincoAdapter(Context mcontext) {
        this.mcontext = mcontext;
    }

    public void setGachincoBean(GachincoBean gachincoBean) {
        this.gachincoBean = gachincoBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (gachincoBean == null) {
            return 0;
        }
        return gachincoBean.getResult().getNewslist().size();
    }

    @Override
    public Object getItem(int position) {
        return gachincoBean.getResult().getNewslist().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        //确定布局文件
        if (gachincoBean.getResult().getNewslist().get(position).getMediatype() == 6) {
            return TYPE_THREE_IMAGE;
        } else {
            return TYPE_ONE_IMAGE;
        }
    }


    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //封装类初始化
        NetTool mNetTool = new NetTool();

        View threeView = null;
        View oneView = null;

        newType = getItemViewType(position);
        GachincoBean.ResultBean.NewslistBean bean = gachincoBean.getResult().getNewslist().get(position);


        if (newType == TYPE_ONE_IMAGE) {
            ViewHolder viewholder = null;
            if (convertView == null) {
                //注入一个Smallpic的布局
                oneView = LayoutInflater.from(mcontext).inflate(R.layout.item_recommend_gachinco, null);
                viewholder = new ViewHolder(oneView);
                oneView.setTag(viewholder);
                convertView = oneView;
            } else {
                viewholder = (ViewHolder) convertView.getTag();
            }
            viewholder.title.setText(bean.getTitle());
            viewholder.time.setText(bean.getTime());

            if (bean.getMediatype() == 1) {
                viewholder.replycount.setText(bean.getReplycount() + "评论");
                mNetTool.getImage(bean.getSmallpic(), viewholder.smallpic);
                viewholder.frame.setText("");
                viewholder.frame.setBackgroundResource(0);
            }
            if (bean.getMediatype() == 2) {
                viewholder.replycount.setText(bean.getReplycount() + "评论");
                mNetTool.getImage(bean.getSmallpic(), viewholder.smallpic);
                viewholder.frame.setText("说客");
                viewholder.frame.setBackgroundResource(R.drawable.shape_recommend_textbox);
            }
            if (bean.getMediatype() == 3) {
                viewholder.replycount.setText(bean.getReplycount() + "播放");
                mNetTool.getImage(bean.getSmallpic(), viewholder.smallpic);
                viewholder.frame.setText("");
                viewholder.frame.setBackgroundResource(0);
            }
            if (bean.getMediatype() == 4) {
                viewholder.replycount.setText(bean.getReplycount() + "浏览");
                mNetTool.getImage(bean.getSmallpic(), viewholder.smallpic);
                viewholder.frame.setText("口碑");
                viewholder.frame.setBackgroundResource(R.drawable.shape_recommend_textbox);
            }
            if (bean.getMediatype() == 5) {
                viewholder.replycount.setText(bean.getReplycount() + "回帖");
                mNetTool.getImage(bean.getSmallpic(), viewholder.smallpic);
                viewholder.frame.setText("");
                viewholder.frame.setBackgroundResource(0);
            }
        } else if (newType == TYPE_THREE_IMAGE){
            //注入三个Smallpic的布局
            ViewHolderForImg viewHolderForImg = null;
            if (convertView == null) {
                threeView = LayoutInflater.from(mcontext).inflate(R.layout.item_recommend_gachincothree, null);
                viewHolderForImg = new ViewHolderForImg(threeView);
                threeView.setTag(viewHolderForImg);
                convertView = threeView;
            } else {
                viewHolderForImg = (ViewHolderForImg) convertView.getTag();
            }
            viewHolderForImg.title1.setText(bean.getTitle());
            viewHolderForImg.time1.setText(bean.getTime());
            viewHolderForImg.replycount1.setText(bean.getReplycount() + "图片");
            //判断网址的开头结尾
            String urlString = bean.getIndexdetail();
            int start = urlString.indexOf("http");
            int stop = urlString.indexOf(",");

            String url = urlString.substring(start, stop);
            String other = urlString.substring(stop + 1, urlString.length());

            int startSec = other.indexOf("http");
            int stopSec = other.indexOf(",");

            String urlSec = other.substring(startSec, stopSec);
            String urlThree = other.substring(stopSec + 1, other.length());

            mNetTool.getImage(url, viewHolderForImg.smallpic1);
            mNetTool.getImage(urlSec, viewHolderForImg.smallpic2);
            mNetTool.getImage(urlThree, viewHolderForImg.smallpic3);

        }
        return convertView;
    }

    public class ViewHolder {
        ImageView smallpic;
        TextView title;
        TextView time;
        TextView replycount;
        TextView frame;
        public ViewHolder(View oneView) {
            smallpic = (ImageView) oneView.findViewById(R.id.smallpic_recommend_gachinco_item);
            title = (TextView) oneView.findViewById(R.id.title_recommend_gachinco_item);
            time = (TextView) oneView.findViewById(R.id.time_recommend_gachinco_item);
            replycount = (TextView) oneView.findViewById(R.id.replycount_recommend_gachinco_item);
            frame = (TextView) oneView.findViewById(R.id.frame_recommend_gachinco_item);
        }
    }
    public class ViewHolderForImg {
        ImageView smallpic1;
        ImageView smallpic2;
        ImageView smallpic3;
        TextView title1;
        TextView time1;
        TextView replycount1;

        public ViewHolderForImg(View threeView) {
            smallpic1 = (ImageView) threeView.findViewById(R.id.smallpic_one_recommend_gachincothree_item);
            smallpic2 = (ImageView) threeView.findViewById(R.id.smallpic_two_recommend_gachincothree_item);
            smallpic3 = (ImageView) threeView.findViewById(R.id.smallpic_three_recommend_gachincothree_item);
            title1 = (TextView) threeView.findViewById(R.id.title_recommend_gachincothree_item);
            time1 = (TextView) threeView.findViewById(R.id.time_recommend_gachincothree_item);
            replycount1 = (TextView) threeView.findViewById(R.id.replycount_recommend_gachincothree_item);
        }
    }
}
